<template>
  <form class="base-form" :class="{ 'is-mobile': isMobile }" @submit.prevent="handleSubmit">
    <slot></slot>
    <div v-if="$slots.footer || showDefaultFooter" class="form-footer" :class="{ 'mobile-footer': isMobile }">
      <slot name="footer">
        <div v-if="showDefaultFooter" class="form-btns">
          <button
            v-if="showCancel"
            class="btn btn-secondary"
            type="button"
            :disabled="loading"
            @click="handleCancel"
          >
            {{ cancelText }}
          </button>
          <button
            class="btn btn-primary"
            type="submit"
            :disabled="loading || disabled"
          >
            <span v-if="loading && isMobile" class="btn-loading-icon"></span>
            {{ loading ? loadingText : submitText }}
          </button>
        </div>
      </slot>
    </div>
  </form>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'BaseForm',
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    showDefaultFooter: {
      type: Boolean,
      default: true
    },
    showCancel: {
      type: Boolean,
      default: true
    },
    submitText: {
      type: String,
      default: '提交'
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    loadingText: {
      type: String,
      default: '提交中...'
    },
    fullWidth: {
      type: Boolean,
      default: false
    }
  },
  emits: ['submit', 'cancel'],
  computed: {
    ...mapState('app', ['device']),
    isMobile() {
      return this.device === 'mobile'
    }
  },
  methods: {
    handleSubmit() {
      if (this.loading || this.disabled) return
      this.$emit('submit')
    },
    handleCancel() {
      this.$emit('cancel')
    },
    // 滚动到第一个错误元素
    scrollToError() {
      this.$nextTick(() => {
        const errorEl = this.$el.querySelector('.has-error')
        if (errorEl) {
          errorEl.scrollIntoView({ behavior: 'smooth', block: 'center' })
        }
      })
    }
  }
}
</script>

<style scoped>
.base-form {
  width: 100%;
  margin-bottom: 24px;
}

.form-footer {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--divider);
}

.form-btns {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.btn {
  min-width: 96px;
  height: 40px;
  padding: 0 16px;
  font-size: 14px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-primary {
  background-color: var(--primary-blue);
  color: white;
  border: none;
}

.btn-primary:hover {
  background-color: #1565C0;
}

.btn-primary:active {
  background-color: #0D47A1;
}

.btn-primary:disabled {
  background-color: rgba(25, 118, 210, 0.5);
  cursor: not-allowed;
}

.btn-secondary {
  background-color: var(--background-grey);
  color: var(--text-primary);
  border: 1px solid var(--divider);
  margin-right: 12px;
}

.btn-secondary:hover {
  background-color: #E0E0E0;
}

.btn-secondary:active {
  background-color: #BDBDBD;
}

.btn-secondary:disabled {
  background-color: var(--background-grey);
  color: var(--text-disabled);
  cursor: not-allowed;
}

/* 移动端优化 */
.is-mobile .form-btns {
  justify-content: space-between;
}

.is-mobile .btn-secondary {
  margin-right: 0;
}

.is-mobile .btn {
  min-height: var(--touch-target-size);
  font-size: 16px;
  padding: 0 20px;
  border-radius: var(--border-radius-md);
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 固定底部操作栏 */
.mobile-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 12px 16px;
  margin-top: 0;
  border-top: 1px solid #eee;
  z-index: 100;
  /* 添加安全区域边距，适配iPhone X及以上 */
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
}

.is-mobile .mobile-footer .form-btns {
  max-width: 100%;
}

.is-mobile .mobile-footer .btn {
  min-width: 40%;
}

/* 当底部栏为fixed定位时，为表单内容添加底部空间 */
.is-mobile:has(.mobile-footer) {
  padding-bottom: calc(var(--touch-target-size) + 32px + env(safe-area-inset-bottom, 0));
}

/* 微调按钮间距 */
.is-mobile .form-btns .btn + .btn {
  margin-left: 12px;
}

/* 加载状态图标 */
.btn-loading-icon {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 当按钮处于全宽状态 */
.is-mobile .form-btns.full-width .btn {
  width: 100%;
}
</style> 